package cn.tkb.basic.controller;

import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * fastDFs文件上传和删除接口
 */
@RestController
@RequestMapping("/fdfs")
public class FastDfsController {
    @PostMapping()
    public AjaxResult upload(@RequestPart(required = true ,value = "file") MultipartFile file){
            //获取文件后缀名
        try {
            //获取原始数据名
            String filename = file.getOriginalFilename();
            //获取后缀
            String extname = filename.substring(filename.lastIndexOf("." )+1);
            String path = FastDfsUtil.upload(file.getBytes(), extname);
            return AjaxResult.success().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙稍后重试！！！");
        }

    }
    @DeleteMapping()
    public AjaxResult delete(@RequestParam(required = true ,value = "path") String path){
        //获取文件后缀名
        try {
            String pathTemp = path.substring(1);
            String groupName = pathTemp.substring(0, pathTemp.indexOf("/"));
            String remotePath = pathTemp.substring(pathTemp.indexOf("/") + 1);
            FastDfsUtil.delete(groupName,remotePath);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙稍后重试！！！");
        }

    }
}
